

/* IMAGE GALLERY */
$(document).ready(function () {
    $('.tl').glisse({
        speed: 500,
        changeSpeed: 550,
        effect: 'bounce',
        fullscreen: false
    });
    $('#changefx').change(function () {
        var val = $(this).val();
        $('.tl').each(function () {
            $(this).data('glisse').changeEffect(val);
        });
    });
});

//*********************  Select all items checkbox using jQuery  *********************//	
$(document).ready(function () {
    $("#maincheck").click(function () {
        if ($('#maincheck').attr('checked')) {
            $('.mc').attr('checked', true);
        } else {
            $('.mc').attr('checked', false);
        }
    });
});


// NOTICE!! DO NOT USE ANY OF THIS JAVASCRIPT
// IT'S ALL JUST JUNK FOR OUR DOCS!
// ++++++++++++++++++++++++++++++++++++++++++

!function ($) {

    $(function () {

        // Disable certain links in docs
        $('section [href^=#]').click(function (e) {
            e.preventDefault()
        })

        // make code pretty
        window.prettyPrint && prettyPrint()

        // add-ons
        $('.add-on :checkbox').on('click', function () {
            var $this = $(this)
                    , method = $this.attr('checked') ? 'addClass' : 'removeClass'
            $(this).parents('.add-on')[method]('active')
        })

        // position static twipsies for components page
        if ($(".twipsies a").length) {
            $(window).on('load resize', function () {
                $(".twipsies a").each(function () {
                    $(this)
                            .tooltip({
                                placement: $(this).attr('title')
                                ,
                                trigger: 'manual'
                            })
                            .tooltip('show')
                })
            })
        }

        // add tipsies to grid for scaffolding
        if ($('#grid-system').length) {
            $('#grid-system').tooltip({
                selector: '.show-grid > div'
                ,
                title: function () {
                    return $(this).width() + 'px'
                }
            })
        }

        // fix sub nav on scroll
        var $win = $(window)
                , $nav = $('.subnav')
                , navTop = $('.subnav').length && $('.subnav').offset().top - 40
                , isFixed = 0

        processScroll()

        // hack sad times - holdover until rewrite for 2.1
        $nav.on('click', function () {
            if (!isFixed)
                setTimeout(function () {
                    $win.scrollTop($win.scrollTop() - 47)
                }, 10)
        })

        $win.on('scroll', processScroll)

        function processScroll() {
            var i, scrollTop = $win.scrollTop()
            if (scrollTop >= navTop && !isFixed) {
                isFixed = 1
                $nav.addClass('subnav-fixed')
            } else if (scrollTop <= navTop && isFixed) {
                isFixed = 0
                $nav.removeClass('subnav-fixed')
            }
        }

        // tooltip demo
        $('.tooltip-bottom').tooltip({
            placement: "bottom",
            selector: "a[data-t=tooltip]"
        })
        $('.tooltip-top').tooltip({
            placement: "top",
            selector: "a[data-t=tooltip]"
        })
        $('.tooltip-left').tooltip({
            placement: "left",
            selector: "a[data-t=tooltip]"
        })
        $('.tooltip-right').tooltip({
            placement: "right",
            selector: "a[data-t=tooltip]"
        })


        $('.popover-top').popover({
            placement: "top"
        })
        $('.popover-bottom').popover({
            placement: "bottom"
        })
        $('.popover-left').popover({
            placement: "left"
        })
        $('.popover-right').popover({
            placement: "right"
        })

        // popover demo
        $("a[data-t=popover]")
                .popover()
                .click(function (e) {

                    e.preventDefault()
                })

        // carousel demo
        $('.myCarousel').carousel()

        // javascript build logic
        var inputsComponent = $("#components.download input")
                , inputsPlugin = $("#plugins.download input")
                , inputsVariables = $("#variables.download input")

        // toggle all plugin checkboxes
        $('#components.download .toggle-all').on('click', function (e) {
            e.preventDefault()
            inputsComponent.attr('checked', !inputsComponent.is(':checked'))
        })

        $('#plugins.download .toggle-all').on('click', function (e) {
            e.preventDefault()
            inputsPlugin.attr('checked', !inputsPlugin.is(':checked'))
        })

        $('#variables.download .toggle-all').on('click', function (e) {
            e.preventDefault()
            inputsVariables.val('')
        })

        // request built javascript
        $('.download-btn').on('click', function () {

            var css = $("#components.download input:checked")
                    .map(function () {
                        return this.value
                    })
                    .toArray()
                    , js = $("#plugins.download input:checked")
                    .map(function () {
                        return this.value
                    })
                    .toArray()
                    , vars = {}
            , img = ['glyphicons-halflings.png', 'glyphicons-halflings-white.png']

            $("#variables.download input")
                    .each(function () {
                        $(this).val() && (vars[ $(this).prev().text() ] = $(this).val())
                    })

            $.ajax({
                type: 'POST'
                ,
                url: /\?dev/.test(window.location) ? 'http://localhost:3000' : 'http://bootstrap.herokuapp.com'
                ,
                dataType: 'jsonpi'
                ,
                params: {
                    js: js
                    ,
                    css: css
                    ,
                    vars: vars
                    ,
                    img: img
                }
            })
        })
    })

    // Modified from the original jsonpi https://github.com/benvinegar/jquery-jsonpi
    $.ajaxTransport('jsonpi', function (opts, originalOptions, jqXHR) {
        var url = opts.url;

        return {
            send: function (_, completeCallback) {
                var name = 'jQuery_iframe_' + jQuery.now()
                        , iframe, form

                iframe = $('<iframe>')
                        .attr('name', name)
                        .appendTo('head')

                form = $('<form>')
                        .attr('method', opts.type) // GET or POST
                        .attr('action', url)
                        .attr('target', name)

                $.each(opts.params, function (k, v) {

                    $('<input>')
                            .attr('type', 'hidden')
                            .attr('name', k)
                            .attr('value', typeof v == 'string' ? v : JSON.stringify(v))
                            .appendTo(form)
                })

                form.appendTo('body').submit()
            }
        }
    })

}(window.jQuery)


/* ScrollPane */

//*******************  UI  *******************//
$(function () {

    // Accordion
    $(".accordion").accordion({
        header: "h3"
    });

    // Tabs
    $('.tabs').tabs();

    // Dialog			
    $('.dialog').dialog({
        autoOpen: false,
        width: 600,
        buttons: {
            "Ok": function () {
                $(this).dialog("close");
            },
            "Cancel": function () {
                $(this).dialog("close");
            }
        }
    });

    // Dialog Link
    $('.dialog_link').click(function () {
        $('.dialog').dialog('open');
        return false;
    });

    // Datepicker
    $('.datepicker').datepicker({
        inline: true,
        format: 'dd/mm/yyyy ',
    });
    $('.inline-datepicker').datepicker({
        inline: true
    });

    // Progressbar
    $(".progressbar").progressbar({
        value: 20
    });

    //hover states on the static widgets
    $('.dialog_link, ul#icons li').hover(
            function () {
                $(this).addClass('ui-state-hover');
            },
            function () {
                $(this).removeClass('ui-state-hover');
            }
    );

});

//*******************  MENU LEFT  *******************//
jQuery.fn.initMenu = function () {
    return this.each(function () {
        var theMenu = $(this).get(0);
        $('.acitem', this).hide();
        $('li.expand > .acitem', this).show();
        $('li.expand > .acitem', this).prev().addClass('active');
        $('li a', this).click(
                function (e) {
                    e.stopImmediatePropagation();
                    var theElement = $(this).next();
                    var parent = this.parentNode.parentNode;
                    if ($(parent).hasClass('noaccordion')) {
                        if (theElement[0] === undefined) {
                            window.location.href = this.href;
                        }
                        $(theElement).slideToggle('normal', function () {
                            if ($(this).is(':visible')) {
                                $(this).prev().addClass('active');
                            }
                            else {
                                $(this).prev().removeClass('active');
                            }
                        });
                        return false;
                    }
                    else {
                        if (theElement.hasClass('acitem') && theElement.is(':visible')) {
                            if ($(parent).hasClass('collapsible')) {
                                $('.acitem:visible', parent).first().slideUp('normal',
                                        function () {
                                            $(this).prev().removeClass('active');
                                        }
                                );
                                return false;
                            }
                            return false;
                        }
                        if (theElement.hasClass('acitem') && !theElement.is(':visible')) {
                            $('.acitem:visible', parent).first().slideUp('normal', function () {
                                $(this).prev().removeClass('active');
                            });
                            theElement.slideDown('normal', function () {
                                $(this).prev().addClass('active');
                            });
                            return false;
                        }
                    }
                }
        );
    });
};

$(document).ready(function () {
    $('.menu').initMenu();
});

/* calendar mini */
$(function () {
    window.prettyPrint && prettyPrint();
    $('.dp1').datepicker({
        format: 'mm-dd-yyyy'
    });
    $('.dp2').datepicker();
    $('.dp3').datepicker();


    var startDate = new Date(2012, 1, 20);
    var endDate = new Date(2012, 1, 25);
    $('.dp4').datepicker()
            .on('changeDate', function (ev) {
                if (ev.date.valueOf() > endDate.valueOf()) {
                    $('#alert').show().find('strong').text('The start date can not be greater then the end date');
                } else {
                    $('#alert').hide();
                    startDate = new Date(ev.date);
                    $('#startDate').text($('#dp4').data('date'));
                }
                $('#dp4').datepicker('hide');
            });
    $('.dp5').datepicker()
            .on('changeDate', function (ev) {
                if (ev.date.valueOf() < startDate.valueOf()) {
                    $('#alert').show().find('strong').text('The end date can not be less then the start date');
                } else {
                    $('#alert').hide();
                    endDate = new Date(ev.date);
                    $('#endDate').text($('#dp5').data('date'));
                }
                $('#dp5').datepicker('hide');
            });
});





/* toggle */
$(document).ready(function () {

    $('#toggle-view li').click(function () {

        var text = $(this).children('div.panel');

        if (text.is(':hidden')) {
            text.slideDown('200');
            $(this).children('span').html('-');
        } else {
            text.slideUp('200');
            $(this).children('span').html('+');
        }

    });

});



function checkHELLO(field, rules, i, options) {

    if (field.val() != "HELLO") {

        // this allows to use i18 for the error msgs

        return options.allrules.validate2fields.alertText;

    }

}
